/*****************************************
  NAME: eint.c
  DESC: External interrupt test
  WWW.YCTEK.COM
 *****************************************/

#include <string.h>
#include "def.h"
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "2410slib.h" 

#include "eint.h"

static void __irq Eint0Int(void)
{
    ClearPending(BIT_EINT0);
    Uart_Printf("EINT0 interrupt is occurred.\n");
}

static void __irq Eint1Int(void)
{
    ClearPending(BIT_EINT1);
    Uart_Printf("EINT1 interrupt is occurred.\n");
}

static void __irq Eint2Int(void)
{
    ClearPending(BIT_EINT2);
    Uart_Printf("EINT2 interrupt is occurred.\n");
}

static void __irq Eint3Int(void)
{
    ClearPending(BIT_EINT3);
    Uart_Printf("EINT3 interrupt is occurred.\n");
}

static void __irq Eint11_19(void)
{
    if(rEINTPEND==(1<<11))
    {
	Uart_Printf("EINT11 interrupt is occurred.\n");
	rEINTPEND=(1<<11);
    }
    else if(rEINTPEND==(1<<19))
    {
	Uart_Printf("EINT19 interrupt is occurred.\n");
	rEINTPEND=(1<<19);
    }
    else
    {
	Uart_Printf("rEINTPEND=0x%x\n",rEINTPEND);
	rEINTPEND=(1<<19)|(1<<11);
    }
    ClearPending(BIT_EINT8_23);
}


void Test_Eint(void)
{
    int i;
    int extintMode;

    Uart_Printf("[External Interrupt Test through PF0/1/2/3/11/19]\n");

    Uart_Printf("1.LOW-LEVEL  2.FALLING-EDGE  3.RISING-EDGE  4.BOTH-EDGE\n");
    Uart_Printf("Select the external interrupt type.\n");
    extintMode=Uart_Getch();

    rGPFCON = (rGPFCON & 0xffcc)|(1<<7)|(1<<5)|(1<<3)|(1<<1);		//PF0/1/2/3 = EINT0/1/2/3
    rGPGCON = (rGPGCON & 0xff3fff3f)|(1<<23)|(1<<7);	//PG3/11 = EINT11/19
     
    switch(extintMode)
    {
    case '1':
    rEXTINT0 = (rEXTINT0 & 0xFFFF8000); //EINT0/1/2/3=low level triggered
	rEXTINT1 = (rEXTINT1 & ~(7<<12)) | 0x0<<12; //EINT11=low level triggered
	rEXTINT2 = (rEXTINT2 & ~(7<<12)) | 0x0<<12; //EINT19=low level triggered
    	break;
     case '2':
    rEXTINT0 = (rEXTINT0 & 0xFFFFBBBB | 0x2222); //EINT0/1/2/3=falling edge triggered
	rEXTINT1 = (rEXTINT1 & ~(7<<12)) | 0x2<<12; //EINT11=falling edge triggered
	rEXTINT2 = (rEXTINT2 & ~(7<<12)) | 0x2<<12; //EINT19=falling edge triggered
    	break;
    case '3':
	rEXTINT0 = (rEXTINT0 & 0xFFFFDDDD | 0x4444); //EINT0/1/2/3=falling edge triggered
	rEXTINT1 = (rEXTINT1 & ~(7<<12)) | 0x4<<12; //EINT11=rising edge triggered
	rEXTINT2 = (rEXTINT2 & ~(7<<12)) | 0x4<<12; //EINT19=rising edge triggered
     	break;
    case '4':
	rEXTINT0 = (rEXTINT0 |0x7FFFF); //EINT0/1/2/3=both edge triggered
	rEXTINT1 = (rEXTINT1 & ~(7<<12)) | 0x6<<12; //EINT11=both edge triggered
	rEXTINT2 = (rEXTINT2 & ~(7<<12)) | 0x6<<12; //EINT19=both edge triggered
    	break;
    default:
    	break;
    }

    Uart_Printf("Press the EINT0/1/2/3/11/19 buttons or Press any key to exit.\n");

    pISR_EINT0=(U32)Eint0Int;
    pISR_EINT1=(U32)Eint1Int;
    pISR_EINT2=(U32)Eint2Int;
    pISR_EINT3=(U32)Eint3Int;
    pISR_EINT8_23=(U32)Eint11_19;

    rEINTPEND = 0xffffff;
    rSRCPND = BIT_EINT0|BIT_EINT1|BIT_EINT2|BIT_EINT3|BIT_EINT8_23;
    rINTPND = BIT_EINT0|BIT_EINT1|BIT_EINT2|BIT_EINT3|BIT_EINT8_23;
    
    rEINTMASK=~( (1<<11)|(1<<19) );
    rINTMSK=~(BIT_EINT0|BIT_EINT1|BIT_EINT2|BIT_EINT3|BIT_EINT8_23);
		
    Uart_Getch();
    
    rEINTMASK=0xffffff;
    rINTMSK=BIT_ALLMSK;
}

